<template>
  <div class="code-notes">
    <div class="header">
      <h1>代码笔记</h1>
      <router-link to="/editor/code" class="btn btn-primary">
        <span class="icon">+</span>
        新建代码笔记
      </router-link>
    </div>
    
    <div class="filters">
      <input 
        v-model="searchQuery" 
        type="text" 
        placeholder="搜索代码笔记..." 
        class="input search-input"
      />
      <select v-model="selectedLanguage" class="input">
        <option value="">所有语言</option>
        <option value="javascript">JavaScript</option>
        <option value="typescript">TypeScript</option>
        <option value="vue">Vue</option>
        <option value="react">React</option>
        <option value="python">Python</option>
        <option value="java">Java</option>
        <option value="css">CSS</option>
        <option value="html">HTML</option>
        <option value="sql">SQL</option>
        <option value="bash">Bash</option>
      </select>
    </div>
    
    <div class="notes-grid">
      <div 
        v-for="note in filteredNotes" 
        :key="note.id" 
        class="note-card"
        @click="editNote(note)"
      >
        <div class="note-header">
          <h3>{{ note.title }}</h3>
          <span class="language-tag">{{ note.language }}</span>
        </div>
        <p class="note-description">{{ note.description || '暂无描述' }}</p>
        <div class="note-meta">
          <span class="date">{{ formatRelativeTime(note.updatedAt) }}</span>
          <div class="tags">
            <span v-for="tag in note.tags" :key="tag" class="tag">{{ tag }}</span>
          </div>
        </div>
        <div class="note-actions">
          <button @click.stop="deleteNote(note.id)" class="btn btn-danger">删除</button>
        </div>
      </div>
    </div>
    
    <div v-if="filteredNotes.length === 0" class="empty-state">
      <p>暂无代码笔记</p>
      <router-link to="/editor/code" class="btn btn-primary">创建第一个代码笔记</router-link>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import { useNoteStore } from '@/stores/noteStore';
import { formatRelativeTime } from '@/utils/helpers';
import type { CodeNote } from '@/types';

const router = useRouter();
const noteStore = useNoteStore();

const searchQuery = ref('');
const selectedLanguage = ref('');

const filteredNotes = computed(() => {
  let notes = noteStore.codeNotes as CodeNote[];
  
  if (searchQuery.value) {
    notes = notes.filter(note => 
      note.title.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
      note.description?.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
      note.tags.some(tag => tag.toLowerCase().includes(searchQuery.value.toLowerCase()))
    );
  }
  
  if (selectedLanguage.value) {
    notes = notes.filter(note => note.language === selectedLanguage.value);
  }
  
  return notes.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
});

const editNote = (note: CodeNote) => {
  router.push(`/editor/code/${note.id}`);
};

const deleteNote = (id: string) => {
  if (confirm('确定要删除这个代码笔记吗？')) {
    noteStore.deleteNote(id);
  }
};
</script>

<style lang="scss" scoped>
.code-notes {
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: $spacing-lg;
    
    h1 {
      font-size: $font-size-xxl;
      color: $primary-color;
    }
    
    .btn {
      display: flex;
      align-items: center;
      gap: $spacing-xs;
      
      .icon {
        font-size: $font-size-large;
      }
    }
  }
  
  .filters {
    display: flex;
    gap: $spacing-md;
    margin-bottom: $spacing-lg;
    
    .search-input {
      flex: 1;
    }
    
    select {
      min-width: 150px;
    }
  }
  
  .notes-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: $spacing-md;
    
    .note-card {
      background-color: $secondary-color;
      border: $border-width solid $border-color;
      border-radius: $border-radius;
      padding: $spacing-md;
      cursor: pointer;
      transition: all 0.2s ease;
      
      &:hover {
        border-color: $primary-color;
      }
      
      .note-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: $spacing-sm;
        
        h3 {
          font-size: $font-size-large;
          color: $primary-color;
          margin: 0;
          flex: 1;
        }
        
        .language-tag {
          background-color: $hover-color;
          color: $text-color;
          padding: $spacing-xs $spacing-sm;
          border-radius: $border-radius;
          font-size: $font-size-small;
          white-space: nowrap;
        }
      }
      
      .note-description {
        color: $text-light;
        margin-bottom: $spacing-md;
        line-height: 1.5;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      
      .note-meta {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: $spacing-md;
        
        .date {
          color: $text-light;
          font-size: $font-size-small;
        }
        
        .tags {
          display: flex;
          gap: $spacing-xs;
          
          .tag {
            background-color: $hover-color;
            color: $text-color;
            padding: 2px $spacing-xs;
            border-radius: 2px;
            font-size: $font-size-small;
          }
        }
      }
      
      .note-actions {
        display: flex;
        justify-content: flex-end;
        
        .btn-danger {
          background-color: #ff4757;
          color: $secondary-color;
          border-color: #ff4757;
          
          &:hover {
            background-color: #ff3742;
          }
        }
      }
    }
  }
  
  .empty-state {
    text-align: center;
    padding: $spacing-xl 0;
    color: $text-light;
    
    p {
      margin-bottom: $spacing-lg;
      font-size: $font-size-large;
    }
  }
  
  @media (max-width: $breakpoint-mobile) {
    .header {
      flex-direction: column;
      gap: $spacing-md;
      align-items: stretch;
      
      .btn {
        justify-content: center;
      }
    }
    
    .filters {
      flex-direction: column;
    }
    
    .notes-grid {
      grid-template-columns: 1fr;
    }
  }
}
</style> 